define(['app', 'angular'], function (app, angular) {
    var deps = ['$http', 'BaseService'];

    function resource($http, BaseService) {
        return initInfoAccountResource($http, BaseService.restfulUrl, BaseService.formHeader);
    }

    function initInfoAccountResource($http, url, formHeader) {
        var query = {
            queryPerson: function (userId, timeStamp, pageSize) {
                return $http.get(url + 'conversation/queryPersonConver', {
                    params: {
                        userId: userId,
                        timeStamp: timeStamp,
                        pageSize: pageSize
                    }
                });
            },

            queryGroup: function (groupId, timeStamp, pageSize) {
                return $http.get(url + 'conversation/queryGroupConver', {
                    params: {
                        groupId: groupId,
                        timeStamp: timeStamp,
                        pageSize: pageSize
                    }
                });
            },

            queryTopic: function (topicId, timeStamp, pageSize) {
                return $http.get(url + 'conversation/queryConversationList', {
                    params: {
                        topicId: topicId,
                        timeStamp: timeStamp,
                        pageSize: pageSize
                    }
                });
            }
        };
        return {
            /**
             * 获取最近会话列表
             * @param pageNo
             * @param pageSize
             */
            queryRecentContacts : function(pageNo, pageSize) {
                return $http.get(url + 'contactsController/queryConverContacts', {
                    params: {
                        replyNeed: 1,
                        pageNo: pageNo || 1,
                        pageSize: pageSize || 10
                    }
                });
            },

            queryContacts: function(searchValue, pageNo, pageSize) {
                return $http.get(url + 'contactsController/queryUserContacts', {
                    params: {
                        searchValue: searchValue,
                        pageNo: pageNo || 1,
                        pageSize: pageSize || 10
                    }
                });
            },

            query: function(id, timeStamp, pageSize) {
                if (id.topicId) {
                    return query.queryTopic(id.topicId, timeStamp, pageSize);
                }
                if (id.groupId) {
                    return query.queryGroup(id.groupId, timeStamp, pageSize);
                }
                if (id.userId) {
                    return query.queryPerson(id.userId, timeStamp, pageSize);
                }
                return null;
            },

            send: function(conversation) {
                return $http.post(url + 'conversation/saveConversation', angular.toJson(conversation));
            },

            setConversationReaded: function(topicId) {
                return $http.get(url + 'conversation/setConversationReaded', {
                    params: {
                        topicId: topicId
                    }
                });
            }
        };
    }

    resource.$inject = deps;
    app.lazy.factory('ChatResource', resource);
});
